import {ColumnsType} from "antd/es/table";
import {Button, Popconfirm, Space} from "antd";
import {Link} from "react-router-dom";
import {ellipsisHandler} from "../util/ColumnsUtil";
import {ReimburseDetailsType} from "../type/ReimburseDetailsType";
import {fetchDeleteById} from "../redux/reducer/async/detailsAsyncAction";
import GlobalHandler from "../global/GlobalHandler";

const reimburseDetailsColumns: ColumnsType<ReimburseDetailsType> = [
    {
        dataIndex: 'index',
        title: '序号' ,
        width: 60,
        align: "center" ,
        render: (value, record, index) => {
            return <strong> {index + 1} </strong> ;
        }
    },
    {
        key: 'id',
        dataIndex: 'id' ,
        title: '数据编号',
        width: 110,
        align: "center" ,
    },
    {
        key: 'item',
        dataIndex: 'item' ,
        title: '报销项目',
        width: 110,
        align: "center" ,
    },
    {
        key: 'consumeMoney',
        dataIndex: 'consumeMoney' ,
        title: '报销金额',
        width: 110,
        align: "center" ,
    },
    {
        key: 'consumeDate',
        dataIndex: 'consumeDate' ,
        title: '消费日期',
        width: 110,
        align: "center" ,
    },
    {
        key: 'order',
        dataIndex: 'order' ,
        title: '排序',
        width: 100,
        align: "center" ,
    },
    {
        key: 'remark',
        dataIndex: 'remark' ,
        title: '备注信息',
        width: 110,
        align: "center" ,
        ellipsis: {showTitle: false},
        render: ellipsisHandler
    },
    {
        title: '操作',
        dataIndex: 'option' ,
        align: "center",
        width: 100,
        render: (_, record) => {
            return <Space>
                <Link key={"user-update"} to={'./update/' + record.id}
                      state={{reimburseId: window.history.state.usr.reimburseId,totalAmount: window.history.state.usr.totalAmount}}
                      > 更新 </Link>
                <Popconfirm title={"是否要确认删除?"} onConfirm={async () => deleteHandler(record.id) }>
                    <Button danger={true} style={{padding: 0}} type={"link"} key={"user-delete"}> 删除 </Button>
                </Popconfirm>
            </Space> ;
        }
    }

] ;

const deleteHandler = async (id: number) => {
    let res = await GlobalHandler.getInstance().executeDispatchAsync(fetchDeleteById(id)) ;
    if (res.data > 0){
        GlobalHandler.getInstance().sendSuccessMessage("删除报销记录成功!") ;
    }else {
        GlobalHandler.getInstance().sendErrorMessage("删除报销记录失败!") ;
    }
};
export default reimburseDetailsColumns ;

